
#import "PywTitleView.h"
#import "UIView+Pyw.h"
#import "UIImage+Pyw.h"
#import "UIColor+Pyw.h"

@interface PywTitleView()



@end

@implementation PywTitleView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        UIImageView *titleIcon = [[UIImageView alloc] init];
        titleIcon.image = [UIImage imageWithName:@"img_logo"];
        titleIcon.backgroundColor = [UIColor clearColor];
        [self addSubview:titleIcon];
//        [titleIcon release];
        self.titleIcon = titleIcon;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = PywFont18;
        titleLabel.textColor = [UIColor colorWithHex:0x3c3c3c];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.contentMode = UIViewContentModeCenter;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
//        [titleLabel release];
        self.titleLabel = titleLabel;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

+ (instancetype)titleViewWithTitle:(NSString *)title{
    
    PywTitleView *titleView = [[PywTitleView alloc] init];
    
    titleView.titleLabel.text = title;
    
//    CGSize size = [title sizeWithFont:PywFont18 constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attribute = @{NSFontAttributeName:PywFont18,NSParagraphStyleAttributeName:style.copy};
    
    
   CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:attribute
                                     context:nil].size;
    titleView.titleLabel.width = size.width;

    return titleView;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat iconHeight = self.frame.size.height;
    CGFloat iconWidth = iconHeight;
//    self.titleIcon.frame = CGRectMake(iconWidth * 0.15, iconHeight * 0.15 , iconWidth * 0.7, iconHeight * 0.7);
    
    self.titleLabel.frame = CGRectMake(iconHeight* 0.5, 0, self.titleLabel.width, iconHeight);
    
}

- (void)dealloc
{    
//    [super dealloc];
}
@end
